<template>
  <view class="">
    <!-- 选择收货地址的盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址</button>
    </view>
    <view class="address-info-box" v-else @click="chooseAddress" >
      <view class="row1">
          <view class="row1-left">
            <view class="username">收货人：<text>{{address.userName}}</text></view>
          </view>
          <view class="row1-right">
            <view class="phone">电话：<text>{{address.telNumber}}</text></view>
            <uni-icons type="arrowright" size="16"></uni-icons>
          </view>
      </view>
      <view class="row2">
        <view class="row2-left">收货地址：</view>
        <view class="row2-right">{{addStr}}</view>
      </view>
    </view>
    <!-- 底部的边框线 -->
    <view class="address-border"></view>
  </view>
</template>
<script>
  import { mapState,mapMutations,mapGetters } from 'vuex'
  export default{
    data(){
      return{
      //收货地址
    // address:{}
      }
    },
    methods:{
      ...mapMutations('m_user',['updataAddresss']),
      
      async chooseAddress(){
        // 调用小程序提供的chooseAddress()方法，即可使用选择收货地址的功能
        // 返回值是一个数组，第1项为错误对象，第2项为成功之后的收货地址对象
        const res = await uni.chooseAddress()
        // .catch(err => err)
        // 用户成功的选择了收货地址;
           if( res.errMsg === 'chooseAddress:ok'){
         //  // 为data里面的收货地址对象赋值
          // this.address = res
          
          // 调用store中提供的updataAddresss方法，将address保存到address中
          this.updataAddresss(res)
         }
          //用户如果没有授权获取位置
          if( res.errMsg === 'chooseAddress:fail auth deny'){
            this.reAuth() //调用this.reAuth()方法，向用户重新发起授权申请
          }
      },
      // 让用户重新授权
      async reAuth(){
          const res2 = await uni.showModal({
            content:'检测到你没有打开地址权限，是否设置打开？',
            confirmText:"确认",
            cancelText:"取消"
          })
          // 如果弹框异常直接退出
          // if(err2) return
          
          // 如果用户点击了取消
          if(res2.cancel) return uni.$showMsg('您取消了地址授权！')
          // 点击量确认
          if(res2.confirm) return uni.openSetting({
            // 授权结束需要对授权进一步判断
            success: (settingResult) => {
              // 地址授权的值等于true，提示授权成功
              if(settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
              // 地址授权的值等于false，提示用户’您取消了地址授权
              if(!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了授权')
            }
          })
      }
    },
    // 计算属性
    computed:{
      // 把user模块中的address对象映射到当前组件中石油，代替打data中的address对象
      ...mapState('m_user',['address']),
      // 将m_user模块的的计算属性映射到当前位置使用
      ...mapGetters('m_user',['addStr'])
    }
  }
</script>
<style lang="scss">
  .address-border{
    display: block;
    width: 100%;
    height: 10rpx;
    background-color: #efefef;
  }
  .address-choose-box{
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .address-info-box{
    font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
    
    .row1{
      display: flex;
      justify-content: space-between;
      
      .row1-right{
        display: flex;
        justify-content: space-between;
      }
    }
    .row2{
      display: flex;
      align-items: center;
      margin-top: 10px;
      
      .row2-left{
        white-space: nowrap;
      }
    }
  }
</style>
